<style lang="less">
@import './login.less';
</style>

<template>
  <div class="login">
    <div class="login-con">
      <Card icon="log-in"
            title="欢迎登录"
            :bordered="false">
        <div class="form-con">
          <login-form @on-success-valid="handleSubmit"></login-form>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import LoginForm from '_c/login-form'
import { login, GetRoleBySysUserMenuLists } from '@/api/user'
import { setToken } from '@/libs/util'
export default {
  components: {
    LoginForm
  },
  methods: {
    handleSubmit ({ userName, password }) {
      localStorage.removeItem('userId')
      localStorage.removeItem('UserType')
      localStorage.removeItem('userName')
      localStorage.removeItem('userAccess')
      login({ userName, password }).then(res => {
        if (res.data.success) {
          localStorage.setItem('userName', res.data.data.UserName)
          localStorage.setItem('userId', res.data.data.Id)
          localStorage.setItem('UserType', res.data.data.UserType)
          setToken(res.data.data.UserName)
          this.$Message.success('登录成功！')

          GetRoleBySysUserMenuLists({ UserType: localStorage.getItem('UserType') }).then(res => {
            const menuLists = res.data.data.map(item => item.Menu_Num)
            localStorage.setItem('userAccess', menuLists)
            localStorage.setItem('userAccessMenu', menuLists)
          })

          this.$router.push({
            name: 'home'
          })
        } else {
          this.$Message.info('登录失败！请检查用户名和密码是否错误。')
        }
      })
    }
  }
}
</script>

<style>
</style>
